var app = getApp()
Page({
  data: {
    //shop_domin: app.globalData.domain.substring(0, 24),
    navList: [],
    categoryList: [],
    currentCategory: [],
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    thirdcategory:[]
  },
  
  onLoad: function(options) {
    this.getCatalog();
  },
  getCatalog: function() {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: app.globalData.domain + '/item_cats/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token')
      },
      data:{
        level:0
      },
      success:function(res){
        that.setData({
          navList: res.data,
          currentCategory: res.data[0].child_list,
          thirdcategory: res.data[0].child_list[0].child_list
        });
        wx.hideLoading();
      }
    })



    // util.request(api.CatalogList).then(function (res) {
    //     that.setData({
    //       navList: res.data.categoryList,
    //       currentCategory: res.data.currentCategory
    //     });
    //     wx.hideLoading();
    //   });
    // util.request(api.GoodsCount).then(function (res) {
    //   that.setData({
    //     goodsCount: res.data.goodsCount
    //   });
    // });

  },

  
  getCurrentCategory: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.domain + '/item_cats/'+id+'/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token')
      },
      success:function(res){
        
        that.setData({
          currentCategory: res.data['child_list']

        })
      }
    })
    // util.request(api.CatalogCurrent, { id: id })
    //   .then(function (res) {
    //     that.setData({
    //       currentCategory: res.data.currentCategory
    //     });
    //   });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  getList: function() {
    var that = this;
    // util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.cat_id)
    //   .then(function (res) {
    //     that.setData({
    //       categoryList: res.data,
    //     });
    //   });
  },
  switchCate: function(event) { //转换分类
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.pk == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
  }
})